package com.hhkj.study.admin.user.service.base.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hhkj.study.admin.resource.entity.ResourceCourseVideo;
import com.hhkj.study.admin.resource.service.base.ResourceCourseVideoService;
import com.hhkj.study.admin.user.entity.UserListen;
import com.hhkj.study.admin.user.entity.UserListenGroupPo;
import com.hhkj.study.admin.user.mapper.UserListenMapper;
import com.hhkj.study.admin.user.model.podo.*;
import com.hhkj.study.admin.user.model.res.UserStatisticExcelRes;
import com.hhkj.study.admin.user.service.base.UserListenService;
import com.ruoyi.common.base.biz.service.base.BaseServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
*
* @author SERVER
* @since 2024-09-06
*/
@Service
public class UserListenServiceImpl extends BaseServiceImpl<UserListen, UserListenDo, UserListenPo, UserListenMapper> implements UserListenService {

    @Resource
    private ResourceCourseVideoService resourceCourseVideoService;

    @Override
    public List<UserListen> listByUserCourse(Long userId, Long courseId, Long orderItemId, Long type, Collection<Long> videoIdList) {
        LambdaQueryWrapper<UserListen> userExerciseQuery = new LambdaQueryWrapper<>();
        userExerciseQuery.eq(UserListen::getUserId, userId).eq(UserListen::getCourseId, courseId)
                .eq(UserListen::getOrderItemId, orderItemId).eq(ObjectUtil.isNotNull(type), UserListen::getVideoType, type)
                .in(CollectionUtil.isNotEmpty(videoIdList), UserListen::getVideoId, videoIdList);
        List<UserListen> list = this.list(userExerciseQuery);
        if (CollectionUtil.isNotEmpty(list)) {
            Set<Long> videoIdSet = list.stream().map(UserListen::getVideoId).collect(Collectors.toSet());
            List<ResourceCourseVideo> resourceCourseVideoList = resourceCourseVideoService.listByIds(videoIdSet);
            if (CollectionUtil.isEmpty(resourceCourseVideoList)) {
                return list;
            }
            Map<Long, String> videoIdMap = resourceCourseVideoList.stream().collect(Collectors.toMap(ResourceCourseVideo::getId, ResourceCourseVideo::getUrl));
            list.forEach(item -> item.setVideoUrl(videoIdMap.get(item.getVideoId())));
        }
        return list;
    }


    @Override
    public List<UserListenGroupPo> listGroupByOrderItem(Long userId, List<Long> orderItemIdList, Integer listeningStatus) {
        return this.getBaseMapper().listGroupByOrderItem(userId, orderItemIdList, listeningStatus);
    }

    @Override
    public List<UserStatisticPo> countUserStudyByCondition(UserStatisticDo userStatisticDo) {
        return this.getBaseMapper().countUserStudyByCondition(userStatisticDo);
    }

    @Override
    public List<UserStatisticExcelRes> countUserStudyDetail(UserStatisticExportDo userStatisticExportDo) {
        return this.getBaseMapper().countUserStudyDetail(userStatisticExportDo);
    }
}